﻿
using System.Collections.Specialized;

namespace WebRole1
{
	using System;
	using System.Collections.Generic;
	using System.IO;
	using System.Linq;

	using Microsoft.WindowsAzure;
	using Microsoft.WindowsAzure.StorageClient;

	public class CloudStorage
	{
		private readonly CloudBlobClient blobClient;
		public CloudStorage ()
		{
			CloudStorageAccount account = CloudStorageAccount.FromConfigurationSetting("StorageConnectionString");
			blobClient = account.CreateCloudBlobClient();
		}

		public IList<Uri> GetAllPublicBlobs()
		{
			CloudBlobContainer container = blobClient.GetContainerReference ( "public" );
			return container.ListBlobs ().Select ( x => x.Uri ).ToList ();
		}

		public IList<Uri> GetAllPrivateBlobs()
		{
			var container = blobClient.GetContainerReference("private");
			return container.ListBlobs().Select(x => x.Uri).ToList();
		}

		public void ClearPublicStorage()
		{
			CloudBlobContainer container = blobClient.GetContainerReference("public");
			container.Delete();
		}

		public void ClearPrivateStorage()
		{
			CloudBlobContainer container = blobClient.GetContainerReference("private");
			container.Delete();
		}

		public void AddPublicFile(string fileName , Stream fileStream )
		{
			CloudBlobContainer container = blobClient.GetContainerReference("public");
			CloudBlob blob = container.GetBlobReference ( fileName );

			blob.UploadFromStream ( fileStream );
		}

		public void AddPrivateFile(string fileName, Stream fileStream)
		{
			CloudBlobContainer container = blobClient.GetContainerReference("private");
			var blob = container.GetBlobReference(fileName);

			blob.UploadFromStream(fileStream);
		}
	}
}